<?php
/**
 * Created by PhpStorm.
 * User: 彭奇
 * Date: 2018/9/29
 * Time: 21:44
 */
class Student
{
    private  $pagenum = 10;
    function upd(){
        $db = new mySql();
        //获取提交的数据
        $name = $_POST["name"];
        $sex = $_POST["sex"];
        $no = $_POST["no"];
        $date = $_POST["date"];
        $tel = $_POST["tel"];
        $sql = "UPDATE dc_student SET 
`stud_name` = \"{$name}\",`stud_sex`='{$sex}',`birth_date`='{$date}',`mobile`='{$tel}'
WHERE stud_id = {$no};";
        $res = $db->update($sql);
        echo "修改成功";
    }

    function del(){
        $db = new mySql();
        $id =$_GET['id'];
        $sql_del = "DELETE FROM dc_student  WHERE stud_id = '$id';";
        $res = $db->delete($sql_del);
        if($res > 0){
            echo '<meta http-equiv="refresh" content="2;url=index.php?a=student&f=userlist">';
        }
    }
    function add(){


        $db = new mySql();

        $name = $_POST["name"];
        $sex = $_POST["sex"];
        $no = $_POST["no"];
        $date = $_POST["date"];

//        echo $date;
//        die();
        $tel = $_POST["tel"];

        $sql = "insert into dc_student(stud_name,stud_sex,stud_id,birth_date,mobile) 
VALUES ('{$name}','{$sex}','{$no}','$date','$tel')";

        echo $sql;
        $res = $db->insert($sql);


        if($res > 0){
            echo "添加成功";//跳转到列表
            echo "<meta http-equiv=\"refresh\" content=\"3;url=index.php?a=student&f=userlist\">";

        }else{
            echo "添加失败";
        }

    }



    function userlist(){


        $db = new mySql();

//获取页码
        $page = isset($_GET['page']) ? $_GET['page'] : 1;

//计算Limit的值
        $num = ($page - 1)*$this->pagenum;

//分页查询
        $sql = "select * from dc_student ORDER BY birth_date  DESC limit $num,$this->pagenum";
        $students = $db->select($sql);

//计算总页数 = 总记录 / 每页数量
        $sql2 = "select count(stud_id) as cont from dc_student";
        $count = $db->select($sql2);

//总页数
        $count_page = ceil($count[0]['cont']/$this->pagenum);


        ?>


        <div class="am-progress am-progress-striped am-progress-sm am-active ">
            <div class="am-progress-bar am-progress-bar-secondary"  style="width: 10%"></div>
        </div>

        <a class="am-btn am-btn-primary" href="addStudent.html">添加学生</a>
        <table class="am-table">
            <thead>
            <tr>
                <th>学号</th>
                <th>姓名</th>
                <th>性别</th>
                <th>生日</th>
                <th>入学日期</th>
                <th>手机号码</th>
            </tr>
            </thead>
            <tbody>


            <?php foreach ($students as $stu){ ?>
                <tr>
                    <td><?php echo $stu['stud_id']   ?></td>
                    <td><?php echo $stu['stud_name']   ?></td>
                    <td><?php echo $stu['stud_sex']   ?></td>
                    <td><?php echo $stu['birth_date']   ?></td>
                    <td><?php echo $stu['entry_date']   ?></td>
                    <td><?php echo $stu['mobile']   ?></td>
                    <td>
                        <a href=editStudent.php?name=<?php echo $stu['stud_name'] ?>&stu_id=<?php echo $stu['stud_id'] ?>&tel=<?php echo $stu['mobile'] ?>&sex=<?php echo $stu['stud_sex'] ?>&birth_date=<?php echo $stu['birth_date'] ?>&entry_date=<?php echo $stu['entry_date'] ?>>编辑</a>
                        <a href="index.php?a=student&f=del&id=<?php echo $stu['stud_id'] ?>">删除</a>
                    </td>
                </tr>
            <?php } ?>
            </tbody>
        </table>


        <ul class="am-pagination am-pagination-centered">

            <?php
            $prev = $page - 1;
            echo "<li><a href=\"index.php?a=Student&f=userlist&page=1\">首页</a></li>";
            //判断是否为第一页
            if($page == 1){
                echo "<li class=\"am-disabled\" ><a href='#' >&laquo;</a></li>";
            }else{
                echo "<li ><a href=\"index.php?a=Student&f=userlist&page=$prev\">&laquo;</a></li>";
            }

            echo "<li><a  href=\"index.php?a=Student&f=userlist&page=".($page-2)."\"  > 第".($page-2)."页</a></li>";
            echo "<li><a  href=\"index.php?a=Student&f=userlist&page=".($page-1)."\"  > 第".($page-1)."页</a></li>";
            echo "<li class=\"am-active\" ><a    href=\"index.php?a=Student&f=userlist&page=$page\">第{$page}页</a></li>";
            echo "<li><a  href=\"index.php?a=Student&f=userlist&page=".($page+1)."\">第".($page+1)."页</a></li>";
            echo "<li><a  href=\"index.php?a=Student&f=userlist&page=".($page+1)."\">第".($page+2)."页</a></li>";

            //判断是否为最后一页
            $next = $page + 1;
            if($page == $count_page){
                echo "<li class=\"am-disabled\"><a href=\"#\">&raquo;</a></li>";
            }else{
                echo "<li><a href=\"index.php?a=Student&f=userlist&page=$next\">&raquo;</a></li>";
            }

            echo "<li><a href=\"index.php?a=Student&f=userlist&page=$count_page\">尾页</a></li>";

            ?>
        </ul>


        <?php




    }



}